package com.tepth.busfix.main.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.tepth.busfix.R;
import com.tepth.busfix.adapter.BusListAdapter;
import com.tepth.busfix.base.BaseFragment;
import com.tepth.busfix.base.OkBus;
import com.tepth.busfix.bean.reponse.BusInfo;
import com.tepth.busfix.config.WorkStatusCode;
import com.tepth.busfix.main.alarm.AlarmInfoActivity;
import com.tepth.busfix.main.repair.RepairActivity2;

import java.util.ArrayList;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by 张宇 on 2017/3/14.
 */

public class TabFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener,
        SwipeRefreshLayout.OnRefreshListener {
    private String type;
    private ArrayList<BusInfo> busInfos;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    SweetAlertDialog pDialog;


    public static TabFragment newInstance(String type, ArrayList<BusInfo> busInfos,
                                          boolean isPortrait) {
        Bundle arguments = new Bundle();
        arguments.putString("type", type);
        arguments.putSerializable("list", busInfos);
        arguments.putBoolean("isPortrait", isPortrait);
        TabFragment fragment = new TabFragment();
        fragment.setArguments(arguments);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        type = getArguments().getString("type");
        busInfos = (ArrayList<BusInfo>) getArguments().getSerializable("list");
        boolean isPortrait = getArguments().getBoolean("isPortrait");
        View view = inflater.inflate(R.layout.fragment_tab_layout, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        if (isPortrait) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        } else {
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        }
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {

            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                final BusInfo busInfo = busInfos.get(position);
                switch (busInfo.getWorkStatusCode()) {
                    case WorkStatusCode.UNDER_PLAN:
                        AlarmInfoActivity.startActivity(getActivity(), busInfo, -1, false);
                        break;
                    case WorkStatusCode.FINISH_PLAN:
                        pDialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
                                .setTitleText("已报修")
                                .setContentText("车辆还未进厂")
                                .setConfirmText("确认");
                        pDialog.setCancelable(true);
                        pDialog.show();
                        break;
                    case WorkStatusCode.READY_WORK:
                        pDialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
                                .setTitleText("已进厂")
                                .setContentText("确认维修？")
                                .setConfirmText("确认")
                                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        //TODO 这个逻辑应该属于fragment后期在看咋处理
                                        //  RepairActivity.startActivity(getActivity(), busInfo);
                                        OkBus.getInstance().onEvent(HomeActivity.REGISTER_CODE_CONFIRM_REPAIR, busInfo);
                                        pDialog.dismiss();
                                    }
                                })
                                .setCancelText("取消");
                        pDialog.setCancelable(true);
                        pDialog.show();
                        break;
                    case WorkStatusCode.UNDER_WORK:
                        RepairActivity2.startActivity(getActivity(), true, busInfo);
                        break;
                    case WorkStatusCode.FINISH_WORK:
                    case WorkStatusCode.COMPLETE_WORK:
                        RepairActivity2.startActivity(getActivity(), false, busInfo);
                        break;
                }
            }
        });

        BusListAdapter pullToRefreshAdapter = new BusListAdapter(getActivity(), R.layout.item_bus_info2, busInfos);
        recyclerView.setAdapter(pullToRefreshAdapter);
        return view;
    }


    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
        OkBus.getInstance().onEvent(HomeActivity.REGISTER_CODE_CURRENT_REFRESH);
    }

    @Override
    public void onLoadMoreRequested() {

    }

    @Override
    public void onChange(boolean isPor) {
        if(recyclerView == null){
            return;
        }
        if (isPor) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        }
    }
}
